// pages/topic/detail/detail.js
var app=getApp();
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [{
      id: 1,
      type: 'image',
      src: 'http://a4.qpic.cn/psb?/V10uH3z53mPeWp/4ITxFdQ7aROxKP4boCIxei*m.cc5hohmfric72P6liU!/b/dL8AAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAoAHWAIDJwI!&tl=1&vuin=939682389&tm=1557108000&sce=60-1-1&rf=viewer_4',
      url: "/pages/mine/contact/contact"
    },],
    topic_id :null,
    select:-1,
    rate1: '50%',
    rate2: '50%',
    topic:{},
    comment:[],
    page:1,
    user_id:app.globalData.user_id,
    isMore:false,
    isLoad:false,
    sum:500,
    animation1:false,
    animation2: false
  },

  /**
   * 生命周期函数--监听页面加载
   */  
  getComment(page){
    var that =this
    if(that.data.isMore){
      return
    }
    that.setData({
      isLoad:true
    })
    wx.request({
      url: app.globalData.url + 'comment', // 仅为示例，并非真实的接口地址
      data: {
        user_id:app.globalData.user_id,
        topic_id:that.data.topic_id,
        page: page
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.data.length == 0){
          that.setData({
            isMore:true
          })
          return 
        }
        var comment = that.data.comment
        for(var i =0;i<res.data.data.length;i++)
        {
          comment.unshift(res.data.data[i])
        }
        
        that.setData({
          comment:comment,
          page:page+1,
          isLoad:false
        })
      }
    })
  },
  toVote1(){
    var that=this
    this.setData({
      animation1:true
    })
    setTimeout(function () {
      that.setData({
        animation1: false
      })
    },1000)
    console.log('------vate------')
    console.log(1)
    this.toVote(1)
  },
  toVote2() {
    var that = this
    this.setData({
      animation2: true
    })
    setTimeout(function () {
      that.setData({
        animation2: false
      })
    }, 1000)
    console.log('------vate------')
    console.log(2)
    this.toVote(2)
  },
  toVote(num){
    var that=this
    //如果可以投票
    if(that.data.select==0){
      that.upVote(num)
    }

  },
  upVote(select){
    var that =this
    wx.request({
      url: app.globalData.url + 'vote', // 仅为示例，并非真实的接口地址
      data: {
        user_id:app.globalData.user_id,
        select:select,
        topic_id:that.data.topic_id
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        that.getVote()
        that.getVoteCount()
      }
    })
  },
  //获得得分比例
  getVoteCount() {
    var that = this
    wx.request({
      url: app.globalData.url + 'vote/' + that.data.topic_id, // 仅为示例，并非真实的接口地址
      data: {
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        
        if(res.data.state)
        {
          let r1 = (res.data.num1 / (res.data.num1+res.data.num2) * 100).toFixed(2)
          let r2 = (res.data.num2 / (res.data.num1 + res.data.num2) * 100).toFixed(2)
          
          that.setData({
            rate1:r1+'%',
            rate2: r2 + '%',
            sum: res.data.num1 + res.data.num2
          })
          console.log(that.data.rate2)
        }
      }
    })
  },
  getVote(){
    var that =this
    wx.request({
      url: app.globalData.url + 'vote', // 仅为示例，并非真实的接口地址
      data: {
        user_id:app.globalData.user_id,
        topic_id:that.data.topic_id
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        //xuanze
        if(res.data.state==1)
        {
            that.setData({
              select: res.data.data.select
            })
        }
        else{
          that.setData({
            select: 0
          })
        }
      }
    })
  },
  onLoad: function (options) {
    if (!app.globalData.user_id) {
      wx.switchTab({
        url: '../answer',   //注意switchTab只能跳转到带有tab的页面，不能跳转到不带tab的页面
      })
    }
    var that=this
    if (options){
      let topic = JSON.parse(options.topic);
      console.log(options.topic)
      that.setData({
        topic: topic,
        topic_id: topic.id,
        user_id:app.globalData.user_id
      })
    }
    that.setData({
      comment:[],
      page:1
    })
    content:"",
    that.getVote()
    that.getVoteCount()
    that.getComment(that.data.page)
  },
  formSubmit: function (e) {
    var that = this
    console.log(e.detail.value)
    if (e.detail.value.content.length == 0) {
      wx.showModal({
        content: '不能发送空消息哦！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    }
    else{
      wx.request({
        url: app.globalData.url + 'comment', // 仅为示例，并非真实的接口地址
        data: {
          user_id:that.data.user_id,
          topic_id:that.data.topic_id,
          content: e.detail.value.content
        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          that.setData({
            content:''
          })
          that.onLoad()
    
          
          //that.pageScrollToBottom()
  
        }
      })
    }
  },
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.bottom + 5000
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that =this
    that.getComment(that.data.page)
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})